import React, { createContext, useReducer } from 'react';
// import {createContext} from 'react-activation';

interface Props {
  children: React.ReactNode;
}

interface Action<T = any> {
  type: string;
  payload: T;
}
export interface Filter {
  id?: string;
  oneWidth: number; // 一个表单项宽度
  maxRowNum: number;
  minRowNum: number;
  rowNum: number; // 一行数量，默认5个，
  total: number;
  isExpand: boolean;
  colSizes?: number[]; // 表单项所有colSize
}
type FilterAction = Action<Filter>;
interface Context {
  filter: Filter;
  dispatch: React.Dispatch<FilterAction>;
}
const initFilter: Filter = {
  oneWidth: 304,
  maxRowNum: 5,
  minRowNum: 1,
  rowNum: 5,
  total: 0,
  isExpand: false,
  colSizes: [],
};
export const UPDATE_FILTER = 'UPDATE_FILTER';

export const filterReducer = (state: Filter, action: FilterAction) => {
  switch (action.type) {
    case UPDATE_FILTER:
      return {
        ...state,
        ...action.payload,
      };
    default:
      return initFilter;
  }
};

export const filterContext: any = createContext<Context>({
  filter: initFilter,
  dispatch: () => {},
});

function FilterContext(props: Props) {
  const [filter, dispatch] = useReducer(filterReducer, initFilter);
  return (
    <filterContext.Provider value={{ filter, dispatch }}>
      {props.children}
    </filterContext.Provider>
  );
}

export default FilterContext;
